#include <nice2d/textoption.h>
#include <nice2d/application.h>
#include <nice2d/string.h>
#include <nice2d/components/fontmanager.h>

void TextOption::apply(sf::Text &text, const TextOption &option)
{
    auto manager = GET_COMPONENT(Application::getInstance(), FontManager);
    auto font = manager->loadFromFile(option.fontFile);
    if (font) {
        text.setFont(*font);
    }

    text.setCharacterSize(option.characterSize);
    text.setStyle(option.style);
    text.setFillColor(sf::Color::White);
}

PTree TextOption::serialize()
{
    PTree node;
    node.add("file",fontFile);
    node.add("charSize",characterSize);
    node.add("style",toString(style));
    return node;
}

void TextOption::deserialize(const PTree &node)
{
    fontFile = node.get_value<std::string>("file");
    characterSize = node.get<int>("charSize");
    style = fromString(node.get_value<std::string>("style"),sf::Text::Style::Regular);
}
